function reloadPricelist() {
	var date = $('#ProductionDate');
	LoadPricelist(date.val(), function(){
		date.addClass('loading');
	}, function(req){
		date.removeClass('loading');
		if(req.status == 200) {
			document.pricelist = eval('('+req.responseText+')');
			UpdatePrices();
		}
	});
}
function LoadPricelist(date, before, after) {
	$.ajax({
		url: '/proizvodnja/cenovnik/' + date,
		beforeSend: before,
		complete: function(req){
			after(req);
		}
	})
}
function UpdatePrices() {

	var total = 0;
	$('#product_prices .item').each(function(){
		var amount = parseFloat($(this).find('input.amount').val());  amount = amount>0 ? amount : 0;
		var rabat  = parseFloat($(this).find('input.rabat').val()); rabat  = rabat>0  ? rabat  : 0;

		var product_id = parseInt($(this).find('select').eq(0).val());
		
		var price = {price:0};
		if(document.pricelist) {
			var price = document.pricelist[product_id];
		}
		
		// single price		
		$(this).find('input[type=hidden]').eq(0).val(price.price);
		
		// amount-ed price
		price = amount * price.price;
		
		// calculated price
		var price = price * (1-rabat/100);
		$(this).find('.price').html(number_format(price));
		
		// ... add to total
		total += price;
		
	    /*var p1 = amount * price.price;
	    var p2 = p1 * (1 + tax/100);
		total += p2;

		var price_f = number_format(p2);
		$(this).find('input[type=hidden]').eq(0).val(p1);
		$(this).find('.price').html(price_f);*/
	});
	$('.price-total span').html(number_format(total));
}
function product_prices_onAdd() {
	UpdatePrices();
}
function product_prices_onDelete() {
	UpdatePrices();
}
function loadStoreRabat(id, date, element, complete) {
	var store_id = parseInt(id);
	if(store_id>0) {
		$.ajax({
			url: '/klijenti/rabat/' + store_id + '/' + date,
			beforeSend: function() {
				element.addClass('loading');
			},
			complete: function(req) {
				element.removeClass('loading');
				if(req.status==200) {
					var value = parseFloat(req.responseText);
					value = value>0 ? value : 0;
					element.val(value);
					
					complete = complete ? complete : function(){};
					complete(value);
				}
			}
		});
	}
}
function loadTax(date, element) {
	$.ajax({
		url: '/proizvodnja/porez/' + date,
		beforeSend: function() {
			element.addClass('loading');
		},
		complete: function(req) {
			element.removeClass('loading');
			if(req.status==200) {
				var value = parseFloat(req.responseText);
				value = value>0 ? value : 0;
				element.html(value);
			}
		}
	});
}
